package android.Spider;

import java.util.ArrayList;

import android.app.ListActivity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class PictureConnectionListActivity extends ListActivity
{
	@Override
	public void onCreate(Bundle icicle)
	{
		super.onCreate(icicle);

		InitConnection();
		InitListAdapter();
		
		setListAdapter(m_listAdapter);
	}

	void InitListAdapter() {}
	void InitConnection() { m_connection.Initialize(this); }

	final public void AddListItem(ListItem item) { m_listAdapter.m_lstItems.add(item); }
	final public void SetListItems(ArrayList<ListItem> lstItems) { m_listAdapter.m_lstItems = lstItems; m_listAdapter.notifyDataSetChanged(); }
	final public void ResetItem(int nPos, ListItem item)
	{
		m_listAdapter.m_lstItems.set(nPos, item);
		m_listAdapter.notifyDataSetChanged();
	}

	@Override
	protected void onDestroy()
	{
		if (m_connection != null)
			m_connection.Unregister();

		super.onDestroy();
	}

    XmppConnection m_connection = new XmppConnection() { };

	class ListItem
	{
		String m_strCaption = null;
		String m_strDesc = null;
		String m_strFineDesc = null;

		Drawable m_bmpPicture = null;
		String m_strPictureDesc = null;
	};
	
	class PictureAdapter extends BaseAdapter
    {
        public int getCount() { return m_lstItems.size(); }
        public Object getItem(int position) { return position; }
        public long getItemId(int position) { return position; }

        public View getView(int position, View convertView, ViewGroup parent)
        {
        	if (convertView == null)
        		convertView = getViewInflate().inflate(R.layout.picturelistitem, null, null);

        	// Define the picture
        	ListItem item = m_lstItems.get(position);
        	ImageView img = (ImageView)convertView.findViewById(R.id.PictureItemImage);
        	if (item.m_bmpPicture == null)
        		img.setVisibility(View.GONE);
        	else
        		img.setImageDrawable(item.m_bmpPicture);
        	
        	// Define a picture description
        	TextView tv = (TextView)convertView.findViewById(R.id.PictureItemImageDesc);
        	if (item.m_strPictureDesc == null || item.m_strPictureDesc.length() == 0)
        		tv.setVisibility(View.GONE);
        	else
        		tv.setText(item.m_strPictureDesc);

        	// Define a caption
        	tv = (TextView)convertView.findViewById(R.id.PictureItemCaption);
        	if (item.m_strCaption == null || item.m_strCaption.length() == 0)
        		tv.setVisibility(View.GONE);
        	else
        		tv.setText(item.m_strCaption);

        	// Define a description
        	tv = (TextView)convertView.findViewById(R.id.PictureItemDesc);
        	if (item.m_strDesc == null || item.m_strDesc.length() == 0)
        		tv.setVisibility(View.GONE);
        	else
        		tv.setText(item.m_strDesc);
        	
        	// Define a fine description
        	tv = (TextView)convertView.findViewById(R.id.PictureItemFineDesc);
        	if (item.m_strFineDesc == null || item.m_strFineDesc.length() == 0)
        		tv.setVisibility(View.GONE);
        	else
        		tv.setText(item.m_strFineDesc);

        	return convertView;
        }
        
		private ArrayList<ListItem> m_lstItems = new ArrayList<ListItem>();
    };

    PictureAdapter m_listAdapter = new PictureAdapter();

}
